package com.wontube.smr.dao.bo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.Tool;
import com.wontube.smr.client.model.po.ToolDetail;
import com.wontube.smr.client.model.vo.ToolItems;
import com.wontube.smr.dao.mapper.ToolDetailMapper;
import com.wontube.smr.dao.mapper.ToolMapper;
import com.wontube.smr.pub.DefaultDBHelper;

/**
 * admin dao
 * 
 * @author hewc@wondershare.cn
 * 
 */
public final class ToolDao {
	private ToolDao() {
	}

	public static final int insertTool(Tool tool) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		Date now = Calendar.getInstance().getTime();

		tool.setCreateTime(now);
		tool.setModifyTime(now);
		int result = 1;
		try {
			ToolMapper mapper = session.getMapper(ToolMapper.class);
			if (tool.getId() != null && !tool.getId().equals("")) {
				Tool updateTool = mapper.selectByPrimaryKey(tool.getId());
				updateTool.setLink(tool.getLink());
				updateTool.setThumbnail(tool.getThumbnail());
				updateTool.setSortIndex(tool.getSortIndex());
				updateTool.setDescripiton(tool.getDescripiton());
				updateTool.setTitle(tool.getTitle());
				result = mapper.updateByPrimaryKey(tool);
			} else {
				tool.setId(UUID.randomUUID().toString());
				result = mapper.insert(tool);
			}

			session.commit();
		} finally {
			session.close();
		}
		return result;
	}

	public static final int saveOrUpdateToolDetail(ToolDetail tool) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		Date now = Calendar.getInstance().getTime();

		tool.setCreateTime(now);
		tool.setModifyTime(now);
		int result = 1;
		try {
			ToolDetailMapper mapper = session.getMapper(ToolDetailMapper.class);
			if (tool.getId() != null && !tool.getId().equals("")) {
				ToolDetail updateTool = mapper.selectByPrimaryKey(tool.getId());
				updateTool.setThumbnail(tool.getThumbnail());
				updateTool.setBigPic(tool.getBigPic());
				result = mapper.updateByPrimaryKey(tool);
			} else {
				tool.setId(UUID.randomUUID().toString());
				result = mapper.insert(tool);
			}

			session.commit();
		} finally {
			session.close();
		}
		return result;
	}

	public static final ToolItems selectTool(String type) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		ToolItems toolItems = new ToolItems();
		try {
			QueryExample ex = new QueryExample();
			ex.setOrderByClause("sort_index");
			ex.or().addCriterion("tool_type=", type);
			ToolMapper mapper = session.getMapper(ToolMapper.class);
			List<Tool> tools = mapper.selectByExample(ex);
			toolItems.setItems(tools);
			session.commit();
		} finally {
			session.close();
		}
		return toolItems;
	}

	public static final List<ToolDetail> selectToolDetailById(String toolId) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		List<ToolDetail> toolDetails = new ArrayList<ToolDetail>();
		try {
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("tool_id=", toolId);
			ToolDetailMapper mapper = session.getMapper(ToolDetailMapper.class);
			toolDetails = mapper.selectByExample(ex);
			session.commit();
		} finally {
			session.close();
		}
		return toolDetails;
	}

	public static final ToolItems selectTool(String type, PagenateInfo pageInfo) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		ToolItems toolItems = new ToolItems();
		try {
			QueryExample ex = new QueryExample();
			RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
			ex.setOrderByClause("sort_index");
			ex.or().addCriterion("tool_type=", type);
			ToolMapper mapper = session.getMapper(ToolMapper.class);
			List<Tool> tools = mapper.selectByExample(ex, rowBounds);
			toolItems.setItems(tools);
			session.commit();
		} finally {
			session.close();
		}
		return toolItems;
	}

	public static final Tool selectToolById(String id) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		Tool tool = new Tool();
		try {
			ToolMapper mapper = session.getMapper(ToolMapper.class);
			tool = mapper.selectByPrimaryKey(id);
			session.commit();
		} finally {
			session.close();
		}
		return tool;
	}

	public static final int deleteTool(String id) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		int reslut = 0;
		try {
			ToolMapper mapper = session.getMapper(ToolMapper.class);
			reslut = mapper.deleteByPrimaryKey(id);
			session.commit();
		} finally {
			session.close();
		}
		return reslut;
	}

}
